<template>
  <div class="app-container">
    <!-- header -->
    <div class="filterContainer">
      <el-select v-model="institute_id" placeholder="请输入学院信息" clearable @change="changeI">
        <el-option v-for="item in options" :key="item.institute_id" :label="item.institute_name"
          :value="item.institute_id">
        </el-option>
      </el-select>
      <el-select v-model="grade_id" placeholder="请选择年级" clearable>
        <el-option v-for="item in Goptions" :key="item.grade_id" :label="item.grade_name" :value="item.grade_id">
        </el-option>
      </el-select>
      <el-input v-model="keyword" placeholder="关键字" style="width: 200px; margin-right: 10px" class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
    </div>
    <!-- 表格 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <!-- <el-table-column align="center" label="ClassID" width="200">
        <template slot-scope="scope">
          {{ scope.row.class_id }}
        </template>
      </el-table-column> -->
      <el-table-column label="学院名称" width="200" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.institute_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年级名称" width="200" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.grade_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.class_name }}
        </template>
      </el-table-column>
      <el-table-column label="班级人数" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.class_number }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="230">
        <template slot-scope="{ row, $index }">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            Edit
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row, $index)">
            Delete
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑年级信息弹框 -->
    <el-dialog title="年级信息" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="dialogform">

        <el-form-item v-if="dialogStatus == 'create'" label="学院名称" prop="institute_id">
          <el-select v-model="form.institute_id" placeholder="please select institute" clearable @change="changeI">
            <el-option v-for="item in options" :key="item.institute_id" :label="item.institute_name"
              :value="item.institute_id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="dialogStatus == 'create'" label="年级名称" prop="grade_id">
          <el-select v-model="form.grade_id" placeholder="please select grade" clearable>
            <el-option v-for="item in Goptions" :key="item.grade_id" :label="item.grade_name" :value="item.grade_id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="班级名称" prop="class_name">
          <el-input v-model="form.class_name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
          @click="dialogStatus === 'create' ? createData('dialogform') : confirmedit('dialogform')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页 -->
    <div>
      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
        @pagination="fetchData" />
    </div>
  </div>
</template>

<script>
import {
  getInstitute,
  searchGrade,
  getClass,
  searchClass,
  addClass,
  deleteClass,
  editClass
} from '@/api/infotable'
import Pagination from "@/components/Pagination";

export default {
  components: { Pagination },
  filters: {

  },
  data() {
    return {
      // 列表
      list: null,
      // 加载
      listLoading: true,
      // 搜索
      options: null,
      institute_id: '',
      Goptions: null,
      grade_id: '',
      keyword: '',
      // 分页
      total: 1,
      // 传递的页号
      listQuery: {
        pageNum: 1,
        pageSize: 10,
      },
      // 弹框
      dialogFormVisible: false,
      form: {
        institute_id: '',
        grade_id: '',
        class_name: '',
      },
      // 中间量
      obj: null,
      // 状态
      dialogStatus: "",
      rules: {
        institute_id: [
          { required: true, message: '学院不能为空', trigger: 'blur' }
        ],
        grade_id: [
          { required: true, message: '年级不能为空', trigger: 'blur' }
        ],
        class_name: [
          { required: true, message: '班级不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.fetchData()
    this.fetchInstiData()
    this.fetchGrade()
  },
  methods: {
    // 获取信息
    fetchData() {
      this.listLoading = true
      getClass(this.listQuery).then(response => {
        console.log(response);
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 下拉--获取学院
    fetchInstiData() {
      this.listLoading = true
      getInstitute(this.listQuery).then((response) => {
        this.options = response.data.InList.map(item => {
          return { ...item, grades: response.data.GrList.filter(g => g.institute_id == item.institute_id) }
        })
        console.log(this.options);
        console.log(response.data);
        this.listLoading = false;
      });
    },
    // 根据学院筛选年级
    changeI(val) {
      console.log(val);
      searchGrade(this.listQuery, { institute_id: val, keyword: '' }).then((response) => {
        this.Goptions = response.data.items
        console.log(response.data);
      });
    },
    // 下拉--获取年级
    fetchGrade() {
      this.listLoading = true
      searchGrade(this.listQuery, { institute_id: this.institute_id, keyword: '' }).then((response) => {
        this.Goptions = response.data.items
        console.log(response.data);
        this.listLoading = false;
      });
    },
    // 将内容进行重置
    reset() {
      this.form.grade_id = ''
      this.form.institute_id = ''
      this.form.class_name = ''
    },
    // 添加班级
    handleCreate() {
      this.reset()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 添加班级
    createData(form) {
      console.log(this.form);
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          addClass(this.form)
            .then((response) => {
              console.log(response);
              if (response.code == 200) {
                this.fetchData()
                this.dialogFormVisible = false
              }
            })
        } else {
          this.$message.error('添加失败：存在未填项')
          return false;
        }
      });
    },
    // 搜索
    handleFilter() {
      this.listLoading = true
      searchClass(this.listQuery, { keyword: this.keyword, institute_id: this.institute_id, grade_id: this.grade_id })
        .then((response) => {
          console.log(response);
          this.list = response.data.items
          this.total = response.data.total;
          this.listLoading = false;
        })
    },
    // 编辑
    handleUpdate(row) {
      console.log(row);
      this.dialogStatus = 'edit'
      this.form.grade_id = row.grade_id
      this.form.institute_id = row.institute_id
      this.form.class_name = row.class_name
      this.obj = row
      this.dialogFormVisible = true
    },
    // 编辑确定
    confirmedit(form) {
      console.log(this.form);
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          if (this.obj.class_name == this.form.class_name) {
            this.fetchData()
            this.dialogFormVisible = false
          } else {
            editClass({ ...this.form, class_id: this.obj.class_id })
              .then((response) => {
                console.log(response);
                if (response.code == 200) {
                  this.fetchData()
                  this.dialogFormVisible = false
                }
              })
          }
        } else {
          this.$message.error('修改失败：不能为空')
          return false;
        }
      });
    },
    // 删除
    handleDelete(row) {
      console.log(row);
      this.$confirm('此操作将永久删除该班级, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        deleteClass({ class_id: row.class_id, grade_id: row.grade_id })
          .then((response) => {
            console.log(response);
            if (response.code == 200) {
              this.fetchData()
            }
          })
      });
    },
  },
  mounted() {

  }
}
</script>
<style scoped>
.filterContainer {
  margin-bottom: 30px;
}
</style>
